<?php


namespace app\admin\model;


use think\Db;

class TombareaRowColModel extends BaseModel
{
    protected $name='tombarea_row_col';


    /**
     *  修改墓位状态 是否和墓穴相同数量
     * @param $mwId
     */
    public static function get_tomb_regulation($mwId,$customer_id){
        $info =  TombareaInformationProductModel::where(['tomb_id'=>$mwId])->find();;
        $szcount = ArchivesDeceasedModel::where(['tomb_id'=>$mwId,'customer_id'=>$customer_id])->count();
        if ($info['cate_id']==2){
            $tomb_list = Db::name('tomb_mb')->where(['tomb_id'=>$mwId])->select();
            $regulation = 0;
            foreach ($tomb_list as $k=>&$value){
                $regulation+= $value['regulation'];
            }
        }else{
            $regulation = $info['tomb_regulation'];
        }
        if ($szcount==$regulation){
            self::update(['col_status'=>7],['colID'=>$mwId]);
        }else{
            self::update(['col_status'=>6],['colID'=>$mwId]);
        }
    }

    //墓位视图
    public static function getTombRowColById($tid,$mwid=0){
        $field_row ="r.rowID,t.align,t.row,t.id,t.cate_id";
        $field_col = "c.colID,c.num,c.reacher,c.col_status,p.selling_price,p.tomb_regulation,p.new_pro,c.status";
        $field_col_o= 'o.order_id,o.customer_id,o.contract_sn,o.selling_price,o.shoppingguide';
        $field_col_o.= ',ci.buyer_name';
        $row_list = TombareaRowModel::alias("r")->field($field_row)
            ->leftJoin("dj_tombarea t", "t.id = r.tombarea_id", "left")
            ->where([ 't.id' => $tid, 't.status' => 1])->select();
        $row_arr =[];
        foreach ($row_list as $rk=>&$val_info){
            $col_list = self::alias('c')->field($field_col)
                ->leftJoin("dj_tombarea_information_product p","c.colID = p.tomb_id")
                ->where(['c.rowid'=>$val_info["rowID"]])
                ->order("c.column_num ASC")->select();
            $col_arr=[];
            foreach ($col_list as $kc=>&$value){
                $order_info =  OrderModel::alias("o")->field($field_col_o)
                    ->join("dj_customer_info ci","o.customer_id = ci.id","left")
                    ->where(['o.tomb_id'=>$value['colID'],'o.is_delete'=>0])
                    ->where(['o.refund_state'=>0,'o.deposit_state'=>0,'o.is_delete'=>0])
                    ->find();
                $deceased_name = "";
                $deceasedlist = ArchivesDeceasedModel::field("deceased_name,deceased_id")
                    ->where(["tomb_id"=>$value['colID'],'customer_id'=>$order_info['customer_id']])
                    ->where("move_status",1)
                    ->select();
                if(count($deceasedlist)!=0){
                    foreach($deceasedlist as $k => $v){
                        $deceased_name = $deceased_name.$v['deceased_name'].",";
                    }
                    $deceased_name = substr($deceased_name, 0,strlen($deceased_name)-1);
                }

                $value['customer_id'] = $order_info['customer_id'];
                $value['buyer_name'] = $order_info['buyer_name'];
                $value['order_id'] = $order_info['order_id'];
                $value['contract_sn'] = $order_info['contract_sn'];
                $value['shoppingguide'] = isset($order_info['shoppingguide'])?cmf_get_admin_name($order_info['shoppingguide']):'';
                $value['login_id'] = cmf_get_current_admin_id();
                $value['deceased_name'] = $deceased_name;
                $col_arr[] = $result = $value ;
            }
            $val_info['col'] = $col_arr;
            $row_arr[]=$val_info;
        }
        return $row_arr;
    }
    public function contrabOrderList(){
        //定时
        $this->startTrans();
        try {
            $res = $this->alias("c")->field('o.order_id,c.colID,c.col_status,o.customer_id,o.add_time,o.contract_sn')
                ->join("dj_order o", "c.colID = o.tomb_id", "left")
                ->where(array('c.col_status' => 2, 'o.tomb_typei' => 0, 'o.status' => 0, 'o.deposit_price' => 0))
                ->where('o.add_time', '<=', strtotime('-1 day'))
                ->select();
            foreach ($res as $key => &$value) {
//                OrderModel::where(['order_id'=>$value['order_id']])->delete();
//                OrderGoodsModel::where(['order_id'=>$value['order_id']])->delete();
//                CustomerInfoModel::where(['id'=>$value['customer_id']])->delete();
//                OrderPayLogModel::where(['tomb_id'=>$value['colID']])->delete();
    //           TombareaRowColModel::where(['colID'=>$value['colID']])->delete();

    //           TombareaRowColModel::update(['col_status'=>1],['colID'=>$value['colID']]);
    //           OrderModel::update(['is_delete'=>0,'status'=>1],['order_id'=>$value['order_id']]);
    //           CustomerInfoModel::update(['is_delete'=>1],['id'=>$value['customer_id']]);
    //           ContractModel::update(['status'=>1],['contract_sn'=>$value['contract_sn']]);
            }
            $this->commit();
            return true;
        }catch(\Exception $exception) {
            $this->error = '添加失败';
            $this->rollback();
            return false;
        }
    }

}